package com.envper.controller.auth;

import com.envper.common.domain.req.RegisterRequest;
import com.envper.mysql.domain.Result;
import com.envper.mysql.domain.Status;
import com.envper.mysql.domain.dto.CaptchaDto;
import com.envper.mysql.domain.dto.UserRoleDto;
import com.envper.mysql.domain.req.LoginRequest;
import com.envper.mysql.domain.res.LoginResponse;
import com.envper.security.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
    private final AuthService authService;

    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody LoginRequest loginRequest) {
        LoginResponse loginResponse = authService.login(loginRequest);
        return Result.ok(Status.LOGINSUCCESS, loginResponse);
    }

    @PostMapping("/register")
    public Result<String> register(@RequestBody RegisterRequest registerRequest) {
        authService.register(registerRequest);
        return Result.ok(Status.REGISTERSUCCESS);
    }

    @PostMapping("/logout")
    public Result<String> logout() {
        authService.logout();
        return Result.ok(Status.LOGOUTSUCCESS);
    }

    @GetMapping("/captcha")
    public Result<CaptchaDto> getCaptcha() {
        CaptchaDto captcha = authService.getCaptcha();
        return Result.ok(Status.SUCCESS.getMsg(), captcha);
    }

    @PostMapping("/user/role/add")
    public Result<String> addUserRole(@RequestBody UserRoleDto userRole) {
        authService.saveUserRole(userRole);
        return Result.ok(Status.ROLESAVESUCCESS);
    }

}
